﻿package com.msjt.base;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 封装操作Servlet API的方法
 * @author kw
 *
 */
@SuppressWarnings("serial")
public class BaseAction extends ActionSupport {
	//获取Request对象
	@SuppressWarnings("unchecked")
	public Map<String, Object> getRequest(){
		return (Map<String, Object>)ActionContext.getContext().get("request");
	}
	//获取Session对象
	public Map<String, Object> getSession(){
		return (Map<String, Object>)ActionContext.getContext().getSession();
	}
	//获取ServletContext对象
	public Map<String, Object> getApplication(){
		return (Map<String, Object>)ActionContext.getContext().getApplication();
	}
	//获取Response对象
	public HttpServletResponse getResponse(){
		//使用耦合ServletAPI的方式获取，可以获得输出对象，设置文本格式等等
		HttpServletResponse reponse=ServletActionContext.getResponse();
		reponse.setContentType("text/html;charset=gbk");
		reponse.setCharacterEncoding("gbk");
		return reponse;
	}
	//获取HTTPRequest对象
	public HttpServletRequest getHttpRequest(){
		return ServletActionContext.getRequest();
	}
	//获取HttpSession对象
	public HttpSession getHttpSession(){
		return ServletActionContext.getRequest().getSession();
	}
}
